package com.apache.uct.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.uct.common.entity.Lock;
import com.apache.uct.manager.LockManager;
import com.apache.uct.util.UctStringUtil;

public class LockManagerImpl implements LockManager {

	protected IDao lockDao;
	protected final String entityName = "com.apache.uct.common.entity.Lock";

	public void setLockDao(IDao lockDao) {
		this.lockDao = lockDao;
	}

	public String saveInfo(ParamsVo<Lock> vo) throws BusinessException {
		Lock lock = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="lock_"+infoId;
		lock.setLockId(infoId);
		MethodParam param = new MethodParam("Lock", cacheKey, "", entityName);
		param.setVaule(lock);
		if (lockDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<Lock> vo) throws BusinessException {
		Lock lock = vo.getObj();
		if (Validator.isNotNull(lock.getLockId())) {
			String cacheKey = "";
			//String cacheKey="lock_"+lock.getLockId();
			MethodParam param = new MethodParam("Lock", cacheKey, "", entityName);
			param.setVaule(lock);
			return lockDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<Lock> vo) throws BusinessException {
		String key = vo.getKey();
		if (Validator.isNull(key)) {
			String infoId = vo.getInfoId();
			if (Validator.isNull(infoId)) {
				return false;
			}
			String cacheKey = "";
			//String cacheKey="lock_"+infoId;
			String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
			MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
			param.setInfoId(infoId);
			Lock info = (Lock) lockDao.selectById(param);
			if (Validator.isEmpty(info)) {
				return false;
			}
			param.setVaule(info);//此处需要先将状态值赋值为删除状态
			if ("false".equals(mark)) {//逻辑删除
				param.setKey("Lock");
				return lockDao.edit(param);
			} else {
				param.setParams("lockId", infoId);
				param.setDelete(true);
				return lockDao.delete(param);
			}
		} else {
			String sysId = String.valueOf(vo.getParams("sysId"));
			MethodParam param = new MethodParam("BySysId", "", lockDao.getSql(6), entityName);
			param.setParams("sysId", sysId);
			param.setDelete(true);
			return lockDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<Lock> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="lock_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return lockDao.selectById(param);
	}

	public Object execute(ParamsVo<Lock> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<Lock> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "order_num";
		MethodParam param = setMethodParams(vo, 2, orderBy);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		param.setOrderby(orderBy);
		Page page = lockDao.pageSelect(param);
		return page;
	}

	public List<Lock> getList(ParamsVo<Lock> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "order_num";
		MethodParam param = setMethodParams(vo, 2, orderBy);
		param.setOrderby(orderBy);
		return lockDao.select(param);
	}

	public long countInfo(ParamsVo<Lock> vo) {
		MethodParam param = setMethodParams(vo, 1, "");
		return lockDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<Lock> vo, int type, String orderBy) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(lockDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		Lock lock = vo.getObj();
		if (!Validator.isEmpty(lock)) {
			if (Validator.isNotNull(lock.getLockEname())) {
				sb.append(" and lockEname like '%" + lock.getLockEname() + "%'");
				param.setParams("lockEname", "'%" + lock.getLockEname() + "'%");
			}
			if (Validator.isNotNull(lock.getLockCname())) {
				sb.append(" and lockCname like '%" + lock.getLockCname() + "%'");
				param.setParams("lockCname", "'%" + lock.getLockCname() + "%'");
			}
			if (Validator.isNotNull(lock.getLockPath())) {
				sb.append(" and lockPath like '%" + lock.getLockPath() + "%'");
				param.setParams("lockPath", "'%" + lock.getLockPath() + "%'");
			}
			if (Validator.isNotNull(lock.getLockType())) {
				sb.append(" and lockType =:lockType");
				param.setParams("lockType", lock.getLockType());
			}
			if (Validator.isNotNull(lock.getActId())) {
				sb.append(" and actId =:actId");
				param.setParams("actId", lock.getActId());
			}
			if (Validator.isNotNull(lock.getActFullEname())) {
				sb.append(" and actFullEname =:actFullEname");
				param.setParams("actFullEname", lock.getActFullEname());
			}
			if (Validator.isNotNull(lock.getLockStatus())) {
				sb.append(" and lockStatus =:lockStatus");
				param.setParams("lockStatus", lock.getLockStatus());
			}
			if (Validator.isNotNull(lock.getLockRemark())) {
				sb.append(" and lockRemark =:lockRemark");
				param.setParams("lockRemark", lock.getLockRemark());
			}
			if (Validator.isNotNull(lock.getSysId())) {
				sb.append(" and sysId =:sysId");
				param.setParams("sysId", lock.getSysId());
			}
			if (Validator.isNotNull(lock.getSysCname())) {
				sb.append(" and sysCname =:sysCname");
				param.setParams("sysCname", lock.getSysCname());
			}
			if (Validator.isNotNull(lock.getSysEname())) {
				sb.append(" and sysEname =:sysEname");
				param.setParams("sysEname", lock.getSysEname());
			}
		}
		if (Validator.isNotNull(orderBy)) {
			sb.append(" order by " + UctStringUtil.toCamelNamed(orderBy));
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
